//脚本：观战视角
//by K1 2017.12.03 - 2021.10.1

if(isNil("kk_camCommit"))then{kk_camCommit = true};//

FAR_S_camCreate = {
	private ["_camAttachCoords","_camStaticCoords","_pos"];
	cutText [" ","Black in", 2]; //titleText
	if(isNull FAR_camPlayer)then {FAR_camPlayer = (call FAR_S_getUnits) select 0};
	FAR_camRange = if((vehicle FAR_camPlayer) isKindOf "Man")then[{5},{(sizeOf typeOf vehicle FAR_camPlayer) max 5}];
	FAR_theta = 74;
	FAR_phi = -90;
	if (isNil "FAR_camera") then {
		_pos = getPosVisual vehicle FAR_camPlayer;
		_camAttachCoords = call FAR_S_get_camAttachCoords;
	    _camStaticCoords = [(_pos select 0) + (_camAttachCoords select 0),(_pos select 1) + (_camAttachCoords select 1),(_pos select 2) + (_camAttachCoords select 2)];
		FAR_camera = "camera" camCreate _camStaticCoords;
		FAR_camera cameraEffect ["Internal", "Back"];
		FAR_camera CamSetTarget vehicle FAR_camPlayer;
		FAR_camera camCommit 0;
	};
};

FAR_S_getUnits = {
	private _result = switch (FAR_spectating_type) do {
		case "ALL": {call FAR_S_getAliveUnits};
		case "Friendly": {call FAR_S_getAliveFriendlyUnits};
		case "SIDE": {call FAR_S_getAliveSideUnits};
		case "group": {call FAR_S_getAliveGroupUnits};
	};
	if (count _result == 0) then {_result = [player]};
	_call_AI = player getVariable ["FAR_call_AI", objNull];//
	if (alive _call_AI && {!(_call_AI in _result)}) then {_result pushback _call_AI};
	_result
};

// 获取全部玩家可扮演单位
FAR_S_getAllUnits = {
	private _result = [];
	{if(isPlayer _x)then{_result pushback _x}} forEach playableUnits;
	_result
};

// 获取全部活的单位
FAR_S_getAliveUnits = {
	private ["_allPlayers","_result"];
	_allPlayers = call FAR_S_getAllUnits;
	_result = [];
	{if(alive _x)then{_result pushback _x}} forEach _allPlayers;
	_result
};

// 获取全部友好玩家
FAR_S_getAllFriendlyUnits = {
	private ["_allPlayers","_result","_playerSide","_unitSide"];
	_allPlayers = call FAR_S_getAllUnits;
	_playerSide = player getVariable ["FAR_unit_Side", side player];
	_result = [];
	{
		_unitSide = _x getVariable ["FAR_unit_Side", side _x];
		if ((_playerSide getFriend _unitSide >= .6) && {_unitSide getFriend _playerSide >= .6}) then {_result pushback _x};
	} forEach _allPlayers;
	_result
};

// 获取全部活的友好玩家
FAR_S_getAliveFriendlyUnits = {
	private ["_friendlyUnits","_result"];
	_friendlyUnits = call FAR_S_getAllFriendlyUnits;
	_result = [];
	{if(alive _x)then{_result pushback _x}} forEach _friendlyUnits;
	_result
};

FAR_S_getAllSideUnits = {
	private ["_allPlayers","_result","_playerSide"];
	_allPlayers = call FAR_S_getAllUnits;
	_playerSide = player getVariable ["FAR_unit_Side", side player];
	_result = [];
	{if(_x getVariable ["FAR_unit_Side", side _x] == _playerSide)then{_result pushback _x}} forEach _allPlayers;
	_result
};

FAR_S_getAliveSideUnits = {
	private ["_SideUnits","_result"];
	_SideUnits = call FAR_S_getAllSideUnits;
	_result = [];
	{if(alive _x)then{_result pushback _x}} forEach _SideUnits;
	_result
};

// 获取玩家组全部单位包括AI单位
FAR_S_getAllGroupUnits = {
	private _result = [];
	{_result pushback _x} forEach units player;
	_result
};

// 获取玩家组全部活的单位
FAR_S_getAliveGroupUnits = {
	private ["_groupUnits","_result"];
	_groupUnits = call FAR_S_getAllGroupUnits;
	_result = [];
	{if(alive _x)then{_result pushback _x}} forEach _groupUnits;
	_result
};

// Get camera attach coordinate
FAR_S_get_camAttachCoords = {
	private ["_xC","_yC","_zC"];
	_xC = FAR_camRange * sin(FAR_theta) * cos(FAR_phi);
	_yC = FAR_camRange * sin(FAR_theta) * sin(FAR_phi);
	_zC = FAR_camRange * cos(FAR_theta);
	[_xC,_yC,_zC]
};

FAR_S_dynamicText = {
	private ["_name","_color"];
	if FAR_camMap exitWith {};
	_name = if(alive FAR_camPlayer)then[{name FAR_camPlayer},{"???"}];
	_color = switch (FAR_camPlayer getVariable ["FAR_unit_Side", side FAR_camPlayer]) do {
		case west: {"#2554C7"};
		case east: {"#C11B17"};
		case independent: {"#347C17"};
		case sideEnemy: {"#FFBC00"};
		case civilian: {"#9400d3"};
		default {"#ffffff"};
	};
	[format["<t color='%2' shadow='1' shadowColor='#000000' size = '.7'>%1<br/><t color='#808080' shadow='1' shadowColor='#000000' size = '.6'>A键: 上一个单位<br/>D键: 下一个单位<br/></t>",_name,_color],-1,.773 * safeZoneH + safeZoneY,999999,0,0,170] spawn BIS_fnc_dynamicText; //.89
};

FAR_S_cam_1st = { //单机能第一人称，多人只能跟随单位视角
	cutText [" ","Black in", 2];
	if(isNull FAR_camPlayer)then {FAR_camPlayer = (call FAR_S_getUnits) select 0};
	
	player switchCamera "GUNNER";
	
	if (FAR1stGunner && {cameraView != "GUNNER"}) then [{(vehicle FAR_camPlayer) switchCamera "GUNNER"},{(vehicle FAR_camPlayer) switchCamera FAR_CurrentCameraView2}];
};

FAR_S_camera = {
	if (!isNil "FAR_cam_mod_sw") exitWith {}; //
	private ["_KH","_MH1","_MH2","_camAttachCoords","_commit"];
	//disableserialization;
	FAR_camMap = false;
	FAR_cam_mod_sw = true;
	FAR_camPlayer = player; //objNull
	FAR_CurrentCameraView = cameraView;
	FAR_CurrentCameraView2 = cameraView;
	
	if (FAR_cam_mod || {!FAR_coerce_Spectator}) then [{ //观看模式
		call FAR_S_camCreate;
		false setCamUseTi 0;
		showcinemaborder false;
	},{FAR_cam_mod_sw = false}];

	FAR_camPlayer2 = FAR_camPlayer;
	call FAR_S_dynamicText;
	
	waitUntil {!isNull (findDisplay 46)};
	_KH = (findDisplay 46) displayAddEventHandler ["KeyDown", "_this call FAR_S_onKeyPress;"];
	_MH1 = (findDisplay 46) displayAddEventHandler ["MouseMoving", "_this call FAR_S_onMouseMove;"];
	_MH2 = (findDisplay 46) displayAddEventHandler ["MouseZChanged", "_this call FAR_S_onMouseWheel;"];
	[] spawn {sleep 5; if(FAR_Key_sw)then{"按F1打开帮助" call FAR_F_Text}};
	
	["FAR_S_FrameLoop", "onEachFrame", {
		if (FAR_Key_sw) then [{
			_commit = if(kk_camCommit)then[{.1},{0}];
			if (isNull FAR_camPlayer || {FAR_camPlayer != FAR_camPlayer2}) then {
				if (FAR_cam_mod_sw) then [{call FAR_S_camCreate},{call FAR_S_cam_1st}];
				call FAR_S_dynamicText;
				FAR_camPlayer2 = FAR_camPlayer;
				_commit = 0;
			};
			if (FAR_cam_mod_sw && {!isNull FAR_camPlayer}) then {
				_camAttachCoords = call FAR_S_get_camAttachCoords;
				FAR_camera camSetTarget vehicle FAR_camPlayer;
				FAR_camera camSetRelPos _camAttachCoords;
				FAR_camera camCommit _commit;
			};
		},{
			params ["_KH","_MH1","_MH2"];
			(findDisplay 46) displayRemoveEventHandler ["KeyDown",_KH];
			(findDisplay 46) displayRemoveEventHandler ["MouseMoving",_MH1];
			(findDisplay 46) displayRemoveEventHandler ["MouseZChanged",_MH2];
			
			if (!isNil "FAR_camera") then {
				FAR_camera cameraEffect ["terminate","back"];
				camDestroy FAR_camera;
				FAR_camera = nil;
			};
			
			player switchCamera FAR_CurrentCameraView;
			openMap [false,false];
			call FAR_dynamicBlur;
			["",1,1,3,0,0,170] spawn BIS_fnc_dynamicText;
			["FAR_S_FrameLoop", "onEachFrame"] call BIS_fnc_removeStackedEventHandler;
			FAR_cam_mod_sw = nil;
		}];
	},[_KH,_MH1,_MH2]] call BIS_fnc_addStackedEventHandler;
};

FAR_S_onKeyPress = {
	private ["_handled","_list","_id","_size","_key"];
	scopeName "main";
	_key = _this select 1;
	_handled = false;
	if (!alive player) exitWith {};

	if (!FAR_map_gps && {inputAction "showMap" > 0}) then {
		FAR_camMap = !FAR_camMap;
		openMap [FAR_camMap,FAR_camMap];
		if (FAR_camMap) then {
			mapAnimAdd [0,.1,getPosASL FAR_camPlayer];
			mapAnimCommit;
			["",1,1,3,0,0,170] spawn BIS_fnc_dynamicText;
			0 spawn {waitUntil {!visibleMap}; if(FAR_Key_sw)then{call FAR_S_dynamicText}};
		};
	};
	switch _key do {
		//C key
		case 46: {
			if (!FAR_cam_mod || {!FAR_coerce_Spectator}) exitWith {"视角模式已锁定" call FAR_F_Text};
			FAR_cam_mod_sw = !FAR_cam_mod_sw;
			if (FAR_cam_mod_sw) then [{ //自由
				player switchCamera FAR_CurrentCameraView;
				call FAR_S_camCreate;
				false setCamUseTi 0;
				showcinemaborder false;
				"自由视角" call FAR_F_Text;
			},{ //1st
				call FAR_S_cam_1st;
				"单位视角" call FAR_F_Text;
				if (!isNil "FAR_camera") then {
					FAR_camera cameraEffect ["terminate","back"];
					camDestroy FAR_camera;
					FAR_camera = nil;
				};
			}];
		};
		//N key
		case 49: {
			if (!FAR_cam_mod_sw || {visibleMap}) exitWith {};
			if (isNil "FAR_camNVG") then {FAR_camNVG = true};
			camUseNVG FAR_camNVG;
			FAR_camNVG = !FAR_camNVG;
		};
		//Space
		case 57: {
			if (FAR_cam_mod_sw || {visibleMap}) exitWith {};
			FAR1stGunner = !FAR1stGunner;
			call FAR_S_cam_1st;
		};
		//F1
		case 59: {
			["<t color='#818960' size = '.5' align = 'left'>观战模式按键控制:<br/>N - 自由视角: 切换夜视<br/>M - 全屏地图（可用时）<br/>C - 自由视角/单位视角切换<br/>空格 - 第一人称瞄镜视角切换***<br/>数字键盘Enter - 单位视角: 第一人称/第三人称切换***<br/>鼠标滚轮 - 自由视角镜头拉近拉远<br/>标注***的按键功能受限于观看单位的视角状态<br/>在载具限定位置倒下将强制锁定在自由视角<br/></t>",.051 + safeZoneX,.22 * safeZoneH + safeZoneY,20,1,0,188] spawn BIS_fnc_dynamicText; //-.4 color='#99FF00' 绿色
		};
		//A
		case 30: {
			FAR_CurrentCameraView2 = cameraView;
			_list = call FAR_S_getUnits;
			_size = count _list;
			_id = _list find FAR_camPlayer;
			_id = _id - 1;
			if (_id < 0) then {_id = _size - 1};
			FAR_camPlayer = _list select _id;
		};
		//D 
		case 32: {
			FAR_CurrentCameraView2 = cameraView;
			_list = call FAR_S_getUnits;
			_size = count _list;
			_id = _list find FAR_camPlayer;
			_id = _id + 1;
			if (_id == _size) then {_id = 0};
			FAR_camPlayer = _list select _id;
		};	
	};
	_handled
};

FAR_S_onMouseMove = { //鼠标移动
	if (!FAR_cam_mod_sw || {!FAR_cam_free} || {isNull FAR_camera}) exitWith {};
	FAR_theta = FAR_theta - (_this select 2);
	if (FAR_theta < 20) then {FAR_theta = 20};
	if (FAR_theta > 160) then {FAR_theta = 160};
	FAR_phi = FAR_phi - (_this select 1);
	if (FAR_phi < -270) then {FAR_phi = -270};
	if (FAR_phi > 90) then {FAR_phi = 90};
};

FAR_S_onMouseWheel = { //鼠标滚轮
	if (!FAR_cam_mod_sw || {!FAR_cam_free} || {isNull FAR_camera}) exitWith {if(!FAR_cam_free)then{"已禁用视角调整" call FAR_F_Text}};
	FAR_camRange = if((_this select 1) > 0)then[{FAR_camRange - 1},{FAR_camRange + 1}];
	if (FAR_camRange < 2) then {FAR_camRange = 2};
};

